import { PicTypeEnum } from '@/enums/uploadEnum';
import { PicModel } from 'mc-c2b2m-operator-api/types/sys';
import { spuPicModel } from 'mc-c2b2m-operator-api/types/spu';

/**
 * spu详情数据的处理
 * @param data
 */
export function handleSpuDetails(data: any) {
  handleImgListByPicList(data);
  handleCategoryIdByCategoryList(data);
}

/**
 * 获取picList内url给到imgList
 * @param data
 */
export function handleImgListByPicList(data: any) {
  data.imgList = data.picList?.map?.((item: spuPicModel) => item.url) || [];
  delete data.picList;
}

/**
 * 取分类列表第一个分类为目标分类
 * @param data
 */
export function handleCategoryIdByCategoryList(data: any) {
  data.categoryId = data?.categoryList?.[0]?.id;
  delete data.categoryList;
}

/**
 * 数据提交时，关于picList的处理
 * @param data
 */
export function handlePostPicList(data: any) {
  const picList: PicModel[] = [];
  const { imgList } = data || {};
  imgList?.forEach?.((img: string, index: number) => {
    picList.push({
      url: img,
      id: null,
      sortNo: index,
      type: PicTypeEnum.MASTER,
    });
  });
  delete data.imgList;
  data.picList = picList;
}
